#!/usr/bin/env python
#coding=utf-8
from config import FLOATCOST,TEST_RESULT,ACTUAL_RESULT,FLOAT_THRESHOLD,TOP_ATTR,RESULT_DIRCTORY,MIN_SUP

def read_test_result(file_path):
    file = open( file_path,"r" )
    lines = file.readlines()
    dataset = []
    for i in range(1,len(lines)-4 ):
        lines[i] = lines[i].strip()
        line = lines[i].split(',')
        dataset.append( line[1] )
    return dataset

def read_actual_result(file_path):
    file = open( file_path,"r" )
    lines = file.readlines()
    dataset = []
    for i in range(1,len(lines) ):
        lines[i] = lines[i].strip()
        line = lines[i].split(' ')
        dataset.append([])
        dataset[i-1].append( line[1] )
        dataset[i-1].append( line[2] )
    return dataset

def compare( p_set,t_set ):
    right = 0                   # count of predicted right
    wrong = 0                   # count of predicted wrong
    pr = 0                      # count of predict to respond
    ar =0                       # count of actual to respond
    hit = 0                     # count of predict to respond,and actual respond
    profit = 0.0
    for i in range( len(t_set) ):
        if p_set[i] == '1':
            pr +=1
            if t_set[i][0] == '1':
                hit +=1
        if t_set[i][0] =='1':
            ar +=1
        # 预测结果和实际结果一致
        if p_set[i] == t_set[i][0]:
            right +=1
            profit += float(t_set[i][1])
        else:
            wrong +=1
    profit -= FLOATCOST*(pr)
       
    print "Result of predicton:TOP_ATTR="+str(TOP_ATTR)+",MIN_SUP="+str(MIN_SUP)+",FLOAT_THRESHOLD="+str(FLOAT_THRESHOLD)
    print "Num of records predicted: "+str(right+wrong)
    right_ratio = round( float(100*right)/(right+wrong),2 )
    print "Predict correct ratio: "+str( right_ratio )+"%"
    
    precision = round( float(100*hit)/pr,2 )
    print "Precision of respond prediction: "+str( precision )+"%"
    
    rollback = round( float(100*hit)/ar,2 )
    print "Rollback of respond prediction: "+str( rollback )+"%"
    
    print "Total profit: "+str(profit)   
    aver = round( profit/pr,2 )
    print "Average profit of a mail: "+str( aver )
    # write to file
    result_file_path =  RESULT_DIRCTORY+'stat_t'+str(TOP_ATTR )+'_m'+str(MIN_SUP)+'_f'+str(FLOAT_THRESHOLD)+'.txt' 
    #result_file_path = RESULT_DIRCTORY + 'stat_everyone.txt'
    result_file = open( result_file_path,'w')
    
    result_file.write("Result of predicton:TOP_ATTR="+str(TOP_ATTR)+",MIN_SUP="+str(MIN_SUP)+"FLOAT_THRESHOLD="+str(FLOAT_THRESHOLD)+'\n' ) 
    result_file.write( "Num of records predicted: "+str(right+wrong)+'\n')
    result_file.write("Predict correct ratio: "+str( right_ratio )+"%"+'\n' )
    result_file.write("Precision of respond prediction: "+str( precision )+"%"+'\n' )
    result_file.write( "Rollback of respond prediction: "+str( rollback )+"%"+'\n' )
    result_file.write( "Total profit: "+str(profit)+'\n' )
    result_file.write( "Average profit of a mail: "+str( aver )+'\n' )
    print "You can also view this result in "+result_file_path
    
if __name__ == "__main__":
    p_set = read_test_result( TEST_RESULT )
    t_set = read_actual_result( ACTUAL_RESULT )
    compare( p_set,t_set )
    
